{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }}

{{- $images := .Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*background*" -}}
{{- if not $featured }}{{ $featured = $images.GetMatch "*feature*" }}{{ end -}}
{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}

{{ if and .Params.featureimage (not $featured) }}
  {{- $url:= .Params.featureimage -}}
  {{ $featured = resources.GetRemote $url }}
{{ end }}

{{- if not $featured }}
  {{ with .Site.Params.defaultBackgroundImage }}
    {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }}
      {{ $featured = resources.GetRemote . }}
    {{ else }}
      {{ $featured = resources.Get . }}
    {{ end }}
  {{ end }}
{{ end -}}

{{ $isParentList := eq (.Scratch.Get "scope") "list" }}
{{ $shouldBlur := $.Params.layoutBackgroundBlur | default (or
  (and ($.Site.Params.article.layoutBackgroundBlur | default true) (not $isParentList))
  (and ($.Site.Params.list.layoutBackgroundBlur | default true) ($isParentList))
  )
}}
{{ $shouldAddHeaderSpace := $.Params.layoutBackgroundHeaderSpace | default (or
  (and ($.Site.Params.article.layoutBackgroundHeaderSpace | default true) (not $isParentList))
  (and ($.Site.Params.list.layoutBackgroundHeaderSpace | default true) ($isParentList))
  )
}}

{{- with $featured -}}
  {{ if $shouldAddHeaderSpace | default true }}
    <div id="hero" class="h-[150px] md:h-[200px]"></div>
  {{ end }}
  <div class="fixed inset-x-0 top-0 h-[800px] single_hero_background nozoom">
    {{ $imageURL := .RelPermalink }}
    {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }}
      {{ $imageURL = (.Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x")).RelPermalink }}
    {{ end }}
    <img
      id="background-image"
      src="{{ $imageURL }}"
      alt="Background Image"
      class="absolute inset-0 w-full h-full object-cover">
    <div
      class="absolute inset-0 bg-gradient-to-t from-neutral dark:from-neutral-800 to-transparent mix-blend-normal"></div>
    <div
      class="absolute inset-0 opacity-60 bg-gradient-to-t from-neutral dark:from-neutral-800 to-neutral-100 dark:to-neutral-800 mix-blend-normal"></div>
  </div>

  {{ if $shouldBlur | default false }}
    <div
      id="background-blur"
      class="fixed opacity-0 inset-x-0 top-0 h-full single_hero_background nozoom backdrop-blur-2xl"></div>
    {{ $backgroundBlur := resources.Get "js/background-blur.js" }}
    {{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
    <script
      type="text/javascript"
      src="{{ $backgroundBlur.RelPermalink }}"
      integrity="{{ $backgroundBlur.Data.Integrity }}"
      data-target-id="background-blur"
      data-image-id="background-image"
      data-image-url="{{ .RelPermalink }}"></script>
  {{ end }}
{{- end -}}
